const TeacherModel = require('../db/models/teacher');

class TeacherServer {
  async addTeacherData (data) {
    //检查cid是否存在 check cid is existed
    const tid = data.tid;
    const result = await TeacherModel.findOne({where: { tid }});
    //判断 check
    if (result) {
      //更新 update
      return await TeacherModel.update(data, {where: { tid }})
    }else {
      //创建 create
      return await TeacherModel.create(data);
    }
  }

  async getTeacherData () {
    const data =  await TeacherModel.findAll({
      attributes: {
        exclude: ['teacherImgUrl', 'createdAt', 'updatedAt']
      }  
    });
    return data;
  }

  async changeTeacherStatus (id, status) {
     //公用中间件控制器
     const result =  await TeacherModel.update({ status }, {
      where: { id }
    })
    return result[0];
  }

  async selectStarTeacher (id, isStar) {
    const result =  await TeacherModel.update({ isStar }, {
      where: { id }
    })
    return result[0];
  }
}

module.exports = new TeacherServer();